package com.jinmdz.fmis.api.admin.service;

import com.jinmdz.fmis.api.admin.model.admin.*;
import com.jinmdz.fmis.api.api.model.user.AdminSystemViewItem;
import com.jinmdz.fmis.api.base.BaseService;
import com.jinmdz.fmis.api.model.system.UserItem;
import com.jinmdz.fmis.common.util.BeanUtil;
import com.jinmdz.fmis.common.util.DataUtil;
import com.jinmdz.fmis.core.base.BaseResult;
import com.jinmdz.fmis.dao.admin.dao.AdminDao;
import com.jinmdz.fmis.dao.admin.model.admin.AdminMenuButtonListItem;
import com.jinmdz.fmis.dao.admin.model.admin.AdminMenuListItem;
import com.jinmdz.fmis.dao.admin.model.admin.AdminSystemItem;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/**
 * 系统平台Service
 *
 * @author LiCongLu
 * @date 2020-03-20 11:34
 */
@Service("adminService")
public class AdminService extends BaseService {

    @Resource
    private AdminDao adminDao;

    /**
     * 登录界面加载系统列表
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-03-24 15:16
     */
    public BaseResult<ArrayList<AdminSystemViewItem>> loadSystemList() {
        ArrayList<AdminSystemItem> systemItems = adminDao.listAdminSystemForLogin();
        // 创建返回数据
        ArrayList<AdminSystemViewItem> loadItems = BeanUtil.copy2List(systemItems, AdminSystemViewItem.class);
        return successList(loadItems);
    }

    /**
     * 根据用户权限加载指定系统的菜单
     *
     * @param userItem 当前帐号
     * @param data     系统编码
     * @return
     * @author LiCongLu
     * @date 2020-03-19 11:26
     */
    public BaseResult<ArrayList<VueMenuComponentItem>> loadSysMenuWithSystemCode(UserItem userItem, SystemCodeData data) {
        // 返回特定格式的菜单
        ArrayList<VueMenuComponentItem> loadItems = new ArrayList<>();

        // 按照当前账号获取业务系统主键
        Integer systemId = adminDao.getSystemIdBySystemCode(data.getSystemCode(), userItem.getId());
        if (DataUtil.invalid(systemId)) {
            return failure("当前账号无加载此系统的权限");
        }

        // 查询获取菜单结构
        ArrayList<AdminMenuTreeItem> menuTreeItems = new ArrayList<>();
        // 获取菜单
        ArrayList<AdminMenuListItem> menuListItems = adminDao.listAdminMenuListByUserId(systemId, userItem.getId());
        // 获取按钮
        ArrayList<AdminMenuButtonListItem> buttonListItems = adminDao.listAdminMenuButtonListByUserId(systemId, userItem.getId());

        // 遍历获取返回结构
        for (AdminMenuListItem menuListItem : menuListItems) {
            AdminMenuTreeItem menuTreeItem = BeanUtil.copy2Bean(menuListItem, new AdminMenuTreeItem());
            ArrayList<AdminMenuButtonViewItem> buttons = new ArrayList<>();
            for (AdminMenuButtonListItem buttonListItem : buttonListItems) {
                if (DataUtil.equals(buttonListItem.getMenuId(), menuListItem.getId())) {
                    AdminMenuButtonViewItem buttonViewItem = BeanUtil.copy2Bean(buttonListItem, new AdminMenuButtonViewItem());
                    buttons.add(buttonViewItem);
                }
            }
            menuTreeItem.setButtons(buttons);
            menuTreeItems.add(menuTreeItem);
        }

        // 顶层菜单组件
        String layout = "Layout";

        /**
         * 记录已处理的菜单
         */
        ArrayList<Integer> menuIds = new ArrayList<>();

        // 迭代遍历
        Iterator<AdminMenuTreeItem> iterator = menuTreeItems.iterator();
        while (iterator.hasNext()) {
            AdminMenuTreeItem menuItem = iterator.next();
            // 判断菜单主键
            if (DataUtil.invalid(menuItem.getId()) || menuIds.contains(menuItem.getId())) {
                continue;
            }

            // 判断是否顶级菜单，即父类主键无效
            if (DataUtil.invalid(menuItem.getParentId())) {
                // 解析生成菜单组件
                VueMenuComponentItem componentItem = getVueMenuComponentItem(menuItem);
                menuIds.add(menuItem.getId());
                // 获取子菜单
                componentItem.setChildren(getVueMenuComponentChildren(menuTreeItems, menuIds, menuItem.getId()));
                componentItem.setComponent(layout);
                loadItems.add(componentItem);
            }
        }
        return successList(loadItems);
    }

    /**
     * 按照父菜单主键解析菜单
     *
     * @param menuTreeItems 菜单集合
     * @param menuIds       已处理的菜单主键集合
     * @param parentId      父类主键
     * @return
     * @author LiCongLu
     * @date 2020-03-20 15:21
     */
    private ArrayList<VueMenuComponentItem> getVueMenuComponentChildren(ArrayList<AdminMenuTreeItem> menuTreeItems, ArrayList<Integer> menuIds, Integer parentId) {
        // 创建子菜单集合
        ArrayList<VueMenuComponentItem> children = new ArrayList<>();
        // 迭代遍历
        Iterator<AdminMenuTreeItem> iterator = menuTreeItems.iterator();
        while (iterator.hasNext()) {
            AdminMenuTreeItem menuItem = iterator.next();
            // 判断菜单主键
            if (DataUtil.invalid(menuItem.getId()) || menuIds.contains(menuItem.getId())) {
                continue;
            }

            // 有效菜单且是子菜单
            if (DataUtil.equals(menuItem.getParentId(), parentId)) {
                // 解析创建菜单组件
                VueMenuComponentItem componentItem = getVueMenuComponentItem(menuItem);
                menuIds.add(menuItem.getId());
                // 获取子菜单
                componentItem.setChildren(getVueMenuComponentChildren(menuTreeItems, menuIds, menuItem.getId()));
                children.add(componentItem);
            }
        }
        return children;
    }

    /**
     * 解析成组件
     *
     * @param menuItem 菜单数据
     * @return
     * @author LiCongLu
     * @date 2020-03-20 15:46
     */
    private VueMenuComponentItem getVueMenuComponentItem(AdminMenuTreeItem menuItem) {
        // 解析路由路径
        VueMenuComponentItem componentItem = new VueMenuComponentItem();
        componentItem.setPath(getRoute2Path(menuItem.getMenuRoute()));
        componentItem.setName(menuItem.getMenuRoute());
        componentItem.setRedirect(getRoute2Path(menuItem.getMenuRedirectRoute()));
        // 设置
        if (DataUtil.invalid(componentItem.getRedirect())) {
            componentItem.setComponent(componentItem.getPath());
        }
        // 解析源数据
        VueMenuComponentMetaItem metaItem = new VueMenuComponentMetaItem();
        metaItem.setId(menuItem.getId());
        metaItem.setTitle(menuItem.getMenuName());
        metaItem.setIcon(menuItem.getMenuIcon());
        // 解析按钮显示
        HashMap<String, Boolean> buttons = new HashMap<>(16);
        if (DataUtil.valid(menuItem.getButtons())) {
            for (AdminMenuButtonViewItem buttonItem : menuItem.getButtons()) {
                buttons.put(buttonItem.getButtonName(), DataUtil.valid(buttonItem.getSelected()));
            }
        }
        metaItem.setButtons(buttons);
        componentItem.setMeta(metaItem);
        return componentItem;
    }

    /**
     * 解析路由地址，转成路径
     *
     * @param route 路由地址
     * @return
     * @author LiCongLu
     * @date 2020-03-20 16:24
     */
    private String getRoute2Path(String route) {
        String dot = ".";
        String slash = "/";
        if (DataUtil.valid(route)) {
            // 用/代替.
            String path = route.replace(dot, slash);
            if (!path.startsWith(slash)) {
                return slash + path;
            }
            return path;
        }
        return "";
    }
}
